<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Presensi_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
		$this->CI = get_instance();
	}
	
	//==================================================================
	function get_presensi_harian($id_sekolah, $id_tingkat='',$id_kelas='',$id_smt=''){
		$this->db->select('*');
		$this->db->from('tb_master_sekolah');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('ISAKTIF',1);
		$query_dept = $this->db->get()->row()->ID_DEPARTMENT;
		
		$this->db->select('ID_TA');
		$this->db->from('tb_master_tahun_ajaran');
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('DEPARTMENT',$query_dept);
		$this->db->where('ISAKTIF',1);
		$query_ta = $this->db->get()->row()->ID_TA;
		
		$this->db->select('t.TINGKAT,t.ID_TINGKAT,k.KELAS,k.ID_KELAS,smt.SEMESTER,smt.ID_SMT, p.*, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA, p.KETERANGAN as KET');
		$this->db->from('tb_presensi_hari p'); 
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->join('tb_master_tingkat t','t.ID_TINGKAT=p.ID_TINGKAT');
		$this->db->join('tb_master_kelas k','k.ID_KELAS=p.ID_KELAS');
		$this->db->join('tb_master_semester smt','smt.ID_SMT=p.ID_SEMESTER');
		$this->db->where('p.TAHUN_AJARAN',$query_ta);
		$this->db->where('p.ID_SEKOLAH',$id_sekolah);
		// if($id_tingkat!='' and $id_kelas!='' and $id_smt!=''){
			$this->db->where('p.ID_TINGKAT',$id_tingkat);
			$this->db->where('p.ID_KELAS',$id_kelas);
			$this->db->where('p.ID_SEMESTER',$id_smt);
			// $this->db->where('p.TANGGAL',$tglpresensi);
		// }
		$this->db->group_by('p.TANGGAL'); 
		// $this->db->order_by('p.TANGGAL','DESC'); 
		$this->CI->flexigrid->build_query();
		
		$return['records'] = $this->db->get();
		
		$this->db->select('t.TINGKAT,t.ID_TINGKAT,k.KELAS,k.ID_KELAS,smt.SEMESTER,smt.ID_SMT, p.*, p.ID_SEKOLAH as ID_SEK, s.NAMA_SEKOLAH, a.TAHUN_AJARAN as NAMA_TA, p.TAHUN_AJARAN as ID_TA, p.KETERANGAN as KET');
		$this->db->from('tb_presensi_hari p'); 
		$this->db->join('tb_master_sekolah s','s.ID_SEKOLAH=p.ID_SEKOLAH');
		$this->db->join('tb_master_tahun_ajaran a','a.ID_TA=p.TAHUN_AJARAN');
		$this->db->join('tb_master_tingkat t','t.ID_TINGKAT=p.ID_TINGKAT');
		$this->db->join('tb_master_kelas k','k.ID_KELAS=p.ID_KELAS');
		$this->db->join('tb_master_semester smt','smt.ID_SMT=p.ID_SEMESTER');
		$this->db->where('p.TAHUN_AJARAN',$query_ta);
		$this->db->where('p.ID_SEKOLAH',$id_sekolah);
		// if($id_tingkat!='' and $id_kelas!='' and $id_smt!=''){
			$this->db->where('p.ID_TINGKAT',$id_tingkat);
			$this->db->where('p.ID_KELAS',$id_kelas);
			$this->db->where('p.ID_SEMESTER',$id_smt);
			// $this->db->where('p.TANGGAL',$tglpresensi);
		// }
		$this->db->group_by('p.TANGGAL'); 
		// $this->db->order_by('p.TANGGAL','DESC');
		$this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();
		// $return['record_count'] = 3;

		return $return;
	}
	
	function get_siswa_kelas($id_sekolah, $id_ta, $id_tingkat, $id_kelas){
		$this->db->select('*');
		$this->db->from('tb_siswa_kelas a'); 
		$this->db->join('tb_siswa b','a.NISN=b.NISN','left'); 
		$this->db->where('a.ID_SEKOLAH',$id_sekolah);
		$this->db->where('a.ID_TAHUNAJARAN',$id_ta);
		$this->db->where('a.ID_TINGKAT',$id_tingkat);
		$this->db->where('a.ID_KELAS',$id_kelas);
		$this->db->where('b.ISAKTIF',1);
		$this->db->order_by('a.NISN','ASC'); 
		$query = $this->db->get();
		return $query;
	}
	
	function get_presensi_kelas($id_sekolah, $id_ta, $id_tingkat, $id_kelas){
		$this->db->select('*');
		$this->db->from('tb_presensi_hari a');  
		$this->db->where('a.ID_SEKOLAH',$id_sekolah);
		$this->db->where('a.TAHUN_AJARAN',$id_ta);
		$this->db->where('a.ID_TINGKAT',$id_tingkat);
		$this->db->where('a.ID_KELAS',$id_kelas);
		$query = $this->db->get();
		return $query;
	}
	
	function get_presensiharianID($id_sekolah, $id_ta, $id_tingkat, $id_kelas, $id_semester, $tglpresensi){
		$this->db->select('*');
		$this->db->from('tb_presensi_hari a');  
		$this->db->where('a.ID_SEKOLAH',$id_sekolah);
		$this->db->where('a.TAHUN_AJARAN',$id_ta);
		$this->db->where('a.ID_TINGKAT',$id_tingkat);
		$this->db->where('a.ID_KELAS',$id_kelas);
		$this->db->where('a.ID_SEMESTER',$id_semester);
		$this->db->where('a.TANGGAL',$tglpresensi);
		$query = $this->db->get();
		return $query;
	}
	function insert_presensiharian($data){
		$this->db->insert('tb_presensi_hari',$data);
	}
	
	function update_presensiharian($data,$id_sekolah, $id_ta, $id_tingkat, $id_kelas, $id_semester, $tglpresensi){
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('TAHUN_AJARAN',$id_ta);
		$this->db->where('KODE_MATA_PELAJARAN',$kode_mp);
		$this->db->update('tb_pelajaran',$data);
	}
	
	function delete_presensiharian($id_sekolah, $id_ta, $id_tingkat, $id_kelas, $id_semester, $tglpresensi){
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('TAHUN_AJARAN',$id_ta);
		$this->db->where('ID_TINGKAT',$id_tingkat);
		$this->db->where('ID_KELAS',$id_kelas);
		$this->db->where('ID_SEMESTER',$id_semester);
		$this->db->where('TANGGAL',$tglpresensi);
		$this->db->delete('tb_presensi_hari'); 
	}
	
	function validasi_ph($id_sekolah,$id_ta,$id_tingkat, $id_kelas, $id_semester, $tglpresensi){
		
		//Select table column
		$this->db->select('count(*) as jml, ID_PRESENSI_HARIAN');
		$this->db->from('tb_presensi_hari');
		$this->db->where('TAHUN_AJARAN',$id_ta);
		$this->db->where('ID_SEKOLAH',$id_sekolah);
		$this->db->where('ID_TINGKAT',$id_tingkat);
		$this->db->where('ID_KELAS',$id_kelas);
		$this->db->where('ID_SEMESTER',$id_semester);
		$this->db->where('TANGGAL',$tglpresensi);
		$query = $this->db->get();
		return $query;
	}
	
	function getWaliKelas($id_tingkat, $id_kelas){
		$this->db->select('a.NIP_WALI, b.GELAR_DEPAN, b.NAMA_LENGKAP, b.GELAR_BELAKANG');
		$this->db->from('tb_master_kelas a');
		$this->db->join('tb_pegawai b','a.NIP_WALI=b.NIP','left');
		$this->db->where('a.ID_TAHUNAJARAN',$this->session->userdata('tahunajaran'));
		$this->db->where('a.ID_SEKOLAH',$this->session->userdata('unitid'));
		$this->db->where('a.ID_TINGKAT',$id_tingkat);
		$this->db->where('a.ID_KELAS',$id_kelas);
		$query = $this->db->get();
		return $query;
	}
}

?>
